import os, pygame

from data import *

class Player(pygame.sprite.Sprite):
    def __init__(self, top, left):
        pygame.sprite.Sprite.__init__(self)
        self.images     = [loadImage("bird%d.png" % i) for i in range(1,8)]
        self.image      = self.images[0]
        self.rect       = self.image.get_rect()
        self.rect.top   = top
        self.rect.left  = left
        self.frame      = 0
        self.x_velocity = 0
        self.y_velocity = 0
        
    def update(self):
        self.rect.move_ip( (self.x_velocity, self.y_velocity) )
            
        self.frame += 1
        
        speed       = 10 - self.x_velocity
        self.image  = self.images[self.frame/speed%7]
        
class PlayerDef(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = loadImage("ball.png")
        self.rect = self.image.get_rect()
        #self.rect.midleft = startpos
        self.rect.centery = y
        self.rect.centerx = x
        
    def update (self, height):
        if self.rect.top >= height:
            self.kill()
        else:
            self.rect.move_ip((0, 4))

class Cannon(pygame.sprite.Sprite):
    def __init__(self, left, bottom):
        pygame.sprite.Sprite.__init__(self)
        self.image = loadImage("cannon.png")
        self.rect = self.image.get_rect()
        self.rect.left = left
        self.rect.bottom = bottom
        
    def update(self, left, bottom):
        if self.rect.right < 0:
            self.rect.left = left
            self.rect.bottom = bottom
        else:
            self.rect.move_ip((-1, 0))
            
class Explosion(pygame.sprite.Sprite):
    def __init__(self, pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = loadImage("explosion.png")
        self.rect = self.image.get_rect()
        self.rect.center = pos
        self.frame = 0
        
    def update(self):        
        if self.frame < 30: 
            if self.rect.right > 0:
                self.rect.move_ip((-1, 0))
        else:
            self.kill()
        
        self.frame += 1
        
class CannonDef(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = loadImage("ball.png")
        self.rect = self.image.get_rect()
        self.rect.centery = y
        self.rect.centerx = x
        self.refresh = True
        self.x = 0
        self.y = 0
        self.frame = 0
        self.test = 0.00
        self.tempTest = 0
        self.frames = 0
        self.clock = pygame.time.Clock()
        
    def update(self, screenWidth, playerCenter, cannonCenter):
        if self.refresh == True:
            playerx, playery = playerCenter
            cannonx, cannony = cannonCenter
        
            width = playerx - cannonx
            height = playery - cannony
            
            self.x = width / 100
            self.y = height / 100
            
            #tempX = self.x
            #tempY = self.y
            #print "tempX 1 = "
            #print tempX
            
            #print "tempY 1 = "
            #print tempY
            
            #if tempX < 0:
            #    tempX = (tempX * -1)
            #    print "tempX 2 = "
            #    print tempX
            #if tempY < 0:
            #    tempY = (tempY * -1)
            #    print "tempY 2 = "
            #    print tempY
            
            #if tempX > tempY:
            #    self.test = float(tempX) / float(tempY)
            #    print "test 1 = "
            #    print self.test
            #elif tempY > tempX:
            #    test = float(tempY) / float(tempX)
            #    print "test 2 = "
            #    print self.test
            #else:
            #    self.test = float(tempX) / float(tempY)
            #    print "test 3 = "
            #    print self.test
            
            #self.tempTest = int(round(self.test * 10))
                      
            #self.frames = int(round(90 * self.test))
            self.refresh = False
                     
        #self.clock.tick((self.frames * 10))
            
        #if self.frame < self.tempTest:
        #    self.frame += 1
        #else:
        if self.rect.bottom <= 0:
            self.kill()
            self.refresh = True
        elif self.rect.right <= 0:
            self.kill()
            self.refresh = True
        elif self.rect.left >= screenWidth:
            self.kill()
            self.refresh = True
        else:
            self.rect.move_ip((self.x, self.y))
            #self.frame = 0
            